# -*- coding: utf-8 -*-            
# @Time : 2023/4/6 18:44
# @FileName: handle_md.py
# @Software: PyCharm
# @Author: zhou chen
import os
import sys
from configparser import ConfigParser
if sys.platform == "win32" or sys.platform == "win64":
    # 获取当前项目的目录
    project_path = os.path.dirname(os.path.dirname(__file__))
else:
    project_path = os.path.abspath("..")


def handle_file(base_path, test_type, send_nub, recv_nub, message_nub, group_nub, all_send_nub, run_time):
    asserts = ""
    with open(base_path + "/README.md", "r", encoding="utf-8") as f:
        file_str = f.read()
    if test_type == "MessageChatTest":
        asserts = f"""
### 普通会话
测试方法：私聊消息测试，{send_nub} 人发送消息， {recv_nub} 接收消息，每人发送消息 ： {message_nub}

|内容|结果|说明|
|服务资源|8核16GB||
|发送条数|{all_send_nub}| {send_nub * message_nub}|
|成功率|{round(all_send_nub / (send_nub * message_nub) *100, 2)} %|实际发送的数量与理论发送的数量|
|测试时间|{run_time} 秒||
|发送速度|{round(all_send_nub/run_time, 2)} 条/秒|每一秒的发送数量|
        """
    elif test_type == "GroupTest":
        asserts = f"""
### 超大群聊
测试方法：私聊消息测试，{send_nub} 人发送消息， {recv_nub} 接收消息，向{group_nub}个群中， 每人发送消息 ： {message_nub}

|内容|结果|说明|
|---|---|---|
|服务资源|8核16GB||
|发送条数|{all_send_nub}| {send_nub} * {group_nub} * {message_nub}|
|成功率|{round(all_send_nub / (send_nub * message_nub * group_nub) *100, 2)} %|实际发送的数量与理论发送的数量|
|测试时间|{run_time}秒||
|发送速度|{round(all_send_nub/run_time, 2)} 条/秒|每一秒的发送数量|"""
    elif test_type == "SuperGroupTest":

        asserts = f"""
### 超大群聊
测试方法：私聊消息测试，{send_nub} 人发送消息， {recv_nub} 接收消息，向{group_nub}个群中， 每人发送消息 ： {message_nub}

|内容|结果|说明|
|---|---|---|
|服务资源|8核16GB||
|发送条数|{all_send_nub}| {send_nub} * {group_nub} * {message_nub}|
|成功率|{round(all_send_nub / (send_nub * message_nub * group_nub) *100, 2)} %|实际发送的数量与理论发送的数量|
|测试时间|{run_time} 秒||
|发送速度|{round(all_send_nub/run_time, 2)} 条/秒|每一秒的发送数量|"""
    file_str = file_str + asserts
    with open(base_path + "/README.md", "w", encoding="utf-8") as f:
        f.write(file_str)


class HandleConfig(object):
    def __init__(self, file_name, config_path="/config/"):
        self._cf = ConfigParser()
        # 配置文件的位置， 需要将配置文件放在指定的文件夹下
        self._path = project_path + config_path + file_name
        self._cf.read(self._path, encoding="utf8")

    # 保存配置文件
    def write_cf(self):
        with open(self._path, "w+") as f:
            self._cf.write(f)

    # 获取 配置文件的 section
    def get_section(self):
        return self._cf.sections()

    # 增加 配置文件 section
    def add_section(self, section):
        self._cf.add_section(section)
        self.write_cf()

    # 存储或修改对应 option 的值
    def set_value(self, section, option, value):
        self._cf.set(section, option, value=value)
        self.write_cf()

    # 获取对应option的值
    def get_value(self, section, option):
        return self._cf.get(section, option)

    # 获取section下的 option
    def get_options(self, section):
        return self._cf.options(section)

    def read_all(self):
        return self._cf.items()


